package com.seaboxdata.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.seaboxdata.dao.DataCountInfoDao;
import com.seaboxdata.entity.DataCountInfo;
import com.seaboxdata.service.DataCountInfoService;
import java.util.Comparator;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import org.springframework.util.CollectionUtils;

/**
 * @author
 * @create 2021-11-3 11:08
 **/
@Service("dataCountInfoService")
public class DataCountInfoServiceImpl extends ServiceImpl<DataCountInfoDao, DataCountInfo> implements DataCountInfoService {

    @Autowired
    private DataCountInfoDao dataCountInfoDao;

    @Override
    public List<DataCountInfo> getAccessWayInfo() {
        List<DataCountInfo> list = dataCountInfoDao.getAccessWayInfo();
        return list;
    }

    @Override
    public List<DataCountInfo> getAccessWayData() {
        //前7天日期
        LocalDate weekDayDate = LocalDate.now().minusDays(14);
        String dt = weekDayDate.toString();
        System.out.println("前7天日期：" + weekDayDate);
   /*     QueryWrapper<DataCountInfo> qw = new QueryWrapper<>();
        qw.eq("data_type","access_way");
        qw.ge("data_dt",weekDayDate.toString());
        qw.orderByDesc("data_dt");*/
        List<DataCountInfo> list = dataCountInfoDao.selectDataCountInfoList(dt);
        return list;
    }

    @Override
    public List<DataCountInfo> getHotDataInfo() {
        List<DataCountInfo> list = dataCountInfoDao.getHotDataInfo();
        return list;
    }

    @Override
    public List<DataCountInfo> getHotData() {
        List<DataCountInfo> dataCountInfos = Lists.newArrayList();
        List<DataCountInfo> dataCounts = Lists.newArrayList();
        //当天日期
        QueryWrapper<DataCountInfo> qw = getQueryWrapper("1");
        List<DataCountInfo> list = dataCountInfoDao.selectList(qw);
        if (!CollectionUtils.isEmpty(list)) {
            dataCountInfos = getHotDataListInfo(list);
        } else {
            //前天
            qw = getQueryWrapper("0");
            list = dataCountInfoDao.selectList(qw);
            dataCountInfos = getHotDataListInfo(list);
        }
        dataCountInfos.stream().limit(5).forEach(e -> {
            DataCountInfo dataCount = new DataCountInfo();
            dataCount.setDataType(e.getDataType());
            dataCount.setDataItemType(e.getDataItemType());
            dataCount.setDataDt(e.getDataDt());
            dataCount.setDataCount(String.valueOf(Long.valueOf(e.getDataCount()) / 10000));
            dataCounts.add(dataCount);
        });
        return dataCounts;
    }


    public QueryWrapper<DataCountInfo> getQueryWrapper(String isNow) {
        QueryWrapper<DataCountInfo> qw = new QueryWrapper<>();
        qw.eq("data_type", "hot_data");
        if ("1".equals(isNow)) {
            //今天
            qw.eq("data_dt", LocalDate.now().toString());
        } else {
            //前天
            qw.eq("data_dt", LocalDate.now().minusDays(1).toString());
        }
        qw.orderByDesc("data_dt");
        return qw;
    }


    public List<DataCountInfo> getHotDataListInfo(List<DataCountInfo> list) {
        List<DataCountInfo> dataCountInfoList = Lists.newArrayList();
        if (!CollectionUtils.isEmpty(list)) {
            dataCountInfoList = list.stream().sorted(Comparator.comparing(DataCountInfo::getDataCount))
                    .collect(Collectors.toList());
        }
        return dataCountInfoList;
    }
}
